package bb.lanxing.mvvm.sportsetting

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import bb.lanxing.mvvm.base.viewmodel.BaseViewModel

class SportSettingViewModel @JvmOverloads constructor(
    application: Application,
    private val sportSettingRepository: SportSettingRepository = SportSettingRepository.getInstance()
) : BaseViewModel(application) {
    private val getSettingResult = MutableLiveData<UserSportSetting>()
    private val updateSettingResult = MutableLiveData<UserSportSetting>()

    fun getGetSettingResult(): LiveData<UserSportSetting> {
        return getSettingResult
    }

    fun getUpdateSettingResult(): LiveData<UserSportSetting> {
        return updateSettingResult
    }

    fun updateSportSetting(userId: Long, setting: UserSportSetting) {
        executeMain {
            try {
                sportSettingRepository.updateSportSetting(userId, setting)
            } catch (e: Throwable) {
                e.printStackTrace()
            }
        }
    }

    fun getSportSetting(userId: Long) {
        executeMain {
            try {
                val result = sportSettingRepository.getSportSetting(userId)
                if (result.isSuccess) {
                    result.data?.let {
                        getSettingResult.postValue(it)
                    }
                }
            } catch (e: Throwable) {
                e.printStackTrace()
            }
        }
    }
}
